package homework5;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Hashtable;

class Words {
 
    static String singles[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine","ten"};
    static String teens[] = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    static String tens[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    
    public static void main (String[] args) {
	
	try{
		if (args == null || args.length <1)
		    //if no input
		    msgForInvalidInput();
		else if(!isInteger(args[0]))
		    //if input not integer
		    msgForInvalidInput();
		else if (Integer.parseInt(args[0]) <= 0 || Integer.parseInt(args[0])>=1000 )
		    //if input not between 1 to 999
		    msgForInvalidInput();
		else
		    //valid input
		    System.out.println(threeDigitsAsWords(Integer.parseInt(args[0])) + "\n" + threeDigisAsWordsViaHashtable(Integer.parseInt(args[0])));
		    
	}catch (NumberFormatException e){
	    msgForInvalidInput();
	}
    }
    
    public static boolean isInteger(String input){
	return Pattern.compile("[0-9]*").matcher(input).matches();
    }
    
    public static void msgForInvalidInput(){
	//Standard error message as per specification
        System.out.println("Usage: words n (where 0 < n < 1000)");
        System.exit(1);
    }
  
    public static String threeDigitsAsWords(int n){
	
	if( n >= 100 ){
	    // three digit input
	    
	    if (n%100 == 0) 
		return "" + singles[n/100] + " hundred";
	    else	   
		return "" + singles[n/100] + " hundred and " + threeDigitsAsWords(n%100);
			    
	}else if( n >= 20){
	    
	    if (n%10 == 0){
		return tens[n/10];
	    }else {
		return tens[n/10] + "-" + threeDigitsAsWords(n%10);
	    }
	    
	}else if( n >= 11 && n <=19 ){
	    
	    return teens[n-10];
	    
	}else {
	    
	    return singles[n];
	}
	
    }

    public static String threeDigisAsWordsViaHashtable(int n){
	
	Hashtable<Integer,String> hTable=new Hashtable<Integer,String>();
	
	hTable.put(new Integer(1), "One");
	hTable.put(new Integer(2), "two");
	hTable.put(new Integer(3), "three");
	hTable.put(new Integer(4), "four");
	hTable.put(new Integer(5), "five");
	hTable.put(new Integer(6), "six");
	hTable.put(new Integer(7), "seven");
	hTable.put(new Integer(8), "eight");
	hTable.put(new Integer(9), "nine");
	hTable.put(new Integer(10), "ten");
	hTable.put(new Integer(11), "eleven");
	hTable.put(new Integer(12), "twelve");
	hTable.put(new Integer(13), "thirteen");
	hTable.put(new Integer(14), "fourteen");
	hTable.put(new Integer(15), "fifteen");
	hTable.put(new Integer(16), "sixteen");
	hTable.put(new Integer(17), "seventeen");
	hTable.put(new Integer(18), "eighteen");
	hTable.put(new Integer(19), "nineteen");
	hTable.put(new Integer(20), "twenty");
	hTable.put(new Integer(30), "thirty");
	hTable.put(new Integer(40), "forty");
	hTable.put(new Integer(50), "fifty");
	hTable.put(new Integer(60), "sixty");
	hTable.put(new Integer(70), "seventy");
	hTable.put(new Integer(80), "eighty");
	hTable.put(new Integer(90), "ninety");


	if( n >= 100 ){
	    // three digit input
	    
	    if (n%100 == 0) 
		return "" + hTable.get(n/100) + " hundred";
	    else	   
		return "" + hTable.get(n/100) + " hundred and " + threeDigitsAsWords(n%100);
			    
	}else if( n >= 20){
	    
	    if (n%10 == 0){
		return hTable.get(n/10);
	    }else {
		return hTable.get(n/10) + "-" + threeDigitsAsWords(n%10);
	    }
	    
	}else if( n >= 11 && n <=19 ){
	    
	    return hTable.get(n-10);
	    
	}else {
	    
	    return hTable.get(n);
	}
	
	
    }
}
